﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RapChieuPhim.DTO;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using RapChieuPhim.BUS;

namespace RapChieuPhim.DAO
{
    class CTDatVeDAO
    {
        public CTDatVeDTO GetByID(int ve, int dat)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@MaVe", ve);
            param[1] = new SqlParameter("@MaDatVe", dat);
            DataTable dt = cn.SqlExcuteQuery("LayCTDatVeTheoID", param);
            CTDatVeDTO CTDatVe = new CTDatVeDTO();

            if (dt.Rows.Count != 0)
            {
                CTDatVe.Ve = VeBUS.GetByID(int.Parse(dt.Rows[0]["MaVe"].ToString()));
                CTDatVe.DatVe = DatVeBUS.GetByID(int.Parse(dt.Rows[0]["MaDatVe"].ToString()));
            }
            return CTDatVe;
        }

        public IList GetAll()
        {
            DataProvider cn = new DataProvider();
            ArrayList array = new ArrayList();
            DataTable dt = cn.SqlExcuteQuery("LayAllCTDatVe");

            if (dt.Rows.Count != 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    CTDatVeDTO CTDatVe = new CTDatVeDTO();
                    CTDatVe.Ve = VeBUS.GetByID(int.Parse(dr["MaVe"].ToString()));
                    CTDatVe.DatVe = DatVeBUS.GetByID(int.Parse(dr["MaDatVe"].ToString()));
                    array.Add(CTDatVe);
                }
            }
            return array;
        }

        public void Insert(CTDatVeDTO CTDatVe)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@MaVe", CTDatVe.Ve);
            param[1] = new SqlParameter("@MaDatVe", CTDatVe.DatVe);
            cn.SqlExcuteNonQuery("InsertCTDatVe", param);
        }

        public void Update(CTDatVeDTO CTDatVe)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@MaVe", CTDatVe.Ve);
            param[1] = new SqlParameter("@MaDatVe", CTDatVe.DatVe);
            cn.SqlExcuteNonQuery("UpdateCTDatVe", param);
        }

        public void Delete(int ve, int dat)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@MaVe", ve);
            param[1] = new SqlParameter("@MaDatVe", dat);
            cn.SqlExcuteNonQuery("DeleteCTDatVe", param);
        }
    }
}
